<?php
/**
 * Created by JetBrains PhpStorm.
 * User: shaoshuai
 * Date: 12-7-15
 * Time: 上午12:15
 * To change this template use File | Settings | File Templates.
 */

include 'boot.php';
session_start();
/**
 * @return array
 */
function getUserList()
{
    return json_decode(file_get_contents('online.list.txt'), true);
}
$userList = getUserList();
$userCount = count($userList);

$user = new User(array('name' => $_SESSION['username']));
session_write_close();

$output = array(
    'success' => true,
    'type' => 0,
);

$seconds = 0;


while (true) {
    //check my new events
    if ($newEventJson = $user->checkNewEvent()) {
        $output['type'] = 2;
        $output['data'] = json_decode($newEventJson, true);
        break;
    }

    //check user list
    $list = getUserList();
    $count = count($list);
    if ($userCount != $count) {
        $output['success'] = true;
        $output['data'] = $list;
        $output['type'] = 1;
        break;
    }

    if ($seconds > 10) {
        break;
    }

    $seconds++;
    sleep(1);
}
echo json_encode($output);
/**
 * check user list
 *
 * check fight request
 *
 * check step
 */